using Microsoft.Xna.Framework.Audio;
using System.Xml;
using ProtectThePieWP7.AdditionalClasses;
using System;
using Microsoft.Xna.Framework.Media;
using System.Windows;
using ProtectThePie;
using ThreeThingGame;

namespace ProtectThePieWP7.GeneralClasses
{
    public enum SongType
    {
        none,
        mainTheme
    }

    public class MySoundEngine
    {
        public bool SoundEnabled;
        public bool MusicEnabled;
        public bool VibrationEnabled;

        private bool UserAllowGameMusic;
        private bool MusicControlQuestionAsked;
        private SongType currentSong;

        #region Constructor

        public MySoundEngine()
        {
            SoundEnabled = true;
            MusicEnabled = false;
            VibrationEnabled = true;
            MusicControlQuestionAsked = false;
            UserAllowGameMusic = false;
        }

        #endregion
        #region Sounds methods

        public void PlaySoundEffect(SoundEffect sound)
        {
            if (SoundEnabled)
            {
                sound.Play();
            }
        }

        public void InvertSound()
        {
            SoundEnabled = !SoundEnabled;
        }

        public void TurnSoundOff()
        {
            SoundEnabled = false;
        }

        public void TurnSoundOn()
        {
            SoundEnabled = true;
        }

        public bool IsSoundOn()
        {
            return SoundEnabled;
        }

        #endregion
        #region Vibration Methods

        public void InvertVibration()
        {
            VibrationEnabled = !VibrationEnabled;
        }

        public void TurnVibrationOff()
        {
            VibrationEnabled = false;
        }

        public void TurnVibrationOn()
        {
            VibrationEnabled = true;
        }

        public bool IsVibrationTurnedOn()
        {
            return VibrationEnabled;
        }

        public void StartVibrating(TimeSpan time)
        {
            if (VibrationEnabled)
                Microsoft.Devices.VibrateController.Default.Start(time);
        }

        #endregion
        #region Song methods

        public void LoadSong(SongType song)
        {
            currentSong = song;
        }

        public void PlayMusic(SongType song)
        {
            return;
            if (MusicEnabled)
            {
                if (!MediaPlayer.GameHasControl && !UserAllowGameMusic) return;

                if (currentSong == SongType.none)
                {
                    currentSong = song;
                    MediaPlayer.Play(Game1.songs[(int)song]);
                    MediaPlayer.IsRepeating = true;
                }
                else
                {
                    if (MediaPlayer.State == MediaState.Paused)
                    {
                        MediaPlayer.Resume();
                    }
                    else
                    {
                        MediaPlayer.Play(Game1.songs[(int)song]);
                        MediaPlayer.IsRepeating = true;
                    }
                }
            }
        }

        public void ResumeMusic()
        {
            if (CanPlayMusic())
            {
                if (currentSong != SongType.none)
                {
                    PlayMusic(currentSong);
                }
            }
        }

        public void PauseMusic()
        {
            if (MediaPlayer.GameHasControl && currentSong != SongType.none)
            {
                MediaPlayer.Pause();
            }
        }

        public void StopMusic()
        {
            if (MediaPlayer.GameHasControl && currentSong != SongType.none)
            {
                MediaPlayer.Stop();
                currentSong = SongType.none;
            }
        }

        public void InvertMusic()
        {
            MusicEnabled = !MusicEnabled;

            if (MusicEnabled)
            {
                ResumeMusic();
            }
            else
            {
                PauseMusic();
            }
        }

        public void TurnMusicOff()
        {
            MusicEnabled = false;

            PauseMusic();
        }

        public void TurnMusicOn()
        {
            //MusicEnabled = true;

            ResumeMusic();
        }

        public bool IsMusicOn()
        {
            return MusicEnabled;
        }

        public void CheckIfMusicCouldBePlayed()
        {
            if (!MediaPlayer.GameHasControl && MusicEnabled)
            {
                MusicEnabled = false;
            }
        }

        private bool CanPlayMusic()
        {
            if (MusicEnabled == false) return false;

            if (MediaPlayer.GameHasControl == false)
            {
                if (MusicControlQuestionAsked == false)
                {
                    if (MessageBox.Show(
                        "You have turned ingame music on, but it seems the music player is busy with something else from your stuff. Are you sure you want to disable your own music and use the ingame one?",
                        "Confirm use of ingame music", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    {
                        MusicEnabled = true;
                        UserAllowGameMusic = true;
                    }
                    else
                    {
                        MusicEnabled = false;
                        UserAllowGameMusic = false;
                    }
                }
                // Set the flag so the user is not asked every time he starts a game.
                MusicControlQuestionAsked = true;
            }
            else
            {
                if (MusicControlQuestionAsked == false)
                {
                    UserAllowGameMusic = true;
                }
            }

            return UserAllowGameMusic;
        }

        #endregion
        #region Saving/Loading

        public void Save(XmlWriter writer)
        {
            writer.WriteStartElement("SoundEngine");
            DataStore.WriteBool(writer, "SoundEnabled", SoundEnabled);
            DataStore.WriteBool(writer, "MusicEnabled", MusicEnabled);
            DataStore.WriteBool(writer, "VibrationEnabled", VibrationEnabled);
            DataStore.WriteBool(writer, "UserAllowGameMusic", UserAllowGameMusic);
            DataStore.WriteBool(writer, "MusicControlQuestionAsked", MusicControlQuestionAsked);
            writer.WriteEndElement();
        }

        public void Load(XmlReader reader)
        {
            do
            {
                // Only detect start elements.
                if (reader.IsStartElement())
                {
                    // Get element name and switch on it.
                    switch (reader.Name)
                    {
                        case "SoundEnabled":
                            SoundEnabled = reader.ReadElementContentAsBoolean();
                            break;
                        case "MusicEnabled":
                            MusicEnabled = reader.ReadElementContentAsBoolean();
                            MusicEnabled = false;
                            break;
                        case "VibrationEnabled":
                            VibrationEnabled = reader.ReadElementContentAsBoolean();
                            break;
                        case "UserAllowGameMusic":
                            UserAllowGameMusic = reader.ReadElementContentAsBoolean();
                            break;
                        case "MusicControlQuestionAsked":
                            MusicControlQuestionAsked = reader.ReadElementContentAsBoolean();
                            break;
                        default:
                            reader.Read();
                            break;
                    }
                }
                else
                {
                    if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "SoundEngine") break;
                    reader.Read();
                }
               
            }
            while (!reader.EOF);            
        }

        #endregion
    }
}
